﻿var DBDialect = "Oracle";// 定义连接数据库的类型，如果是MySQL，配置成var DBDialect="MySQL";

var kdy_list_cfg = {
	nav_first_id : 'nav_first',
	nav_prev_id : 'nav_prev',
	nav_next_id : 'nav_next',
	nav_last_id : 'nav_last',
	pageNumDiv : 'pageNumDiv'
}

/**
 * 选择一行记录，将主键和名称写入parent window
 */
function onRowSelect(trObj) {
	top.dialogback({
				fkValue : trObj.cells[0].innerHTML,
				value : trObj.cells[1].innerText
			})
}

/*
 * 查询结果的显示
 */
function renderTable(tbObj, tbCfg) {
	// 更新导航栏
	renderNaviTable(tbObj, tbCfg);

	// 删除上次的查询结果
	var headChxBox = getObj("headChxBox");
	if (headChxBox != undefined)
		headChxBox.checked = false;
	for (var i = tbObj.rows.length - 1; i > 0; i--)
		tbObj.deleteRow(i);

	var titleRow = tbObj.rows(0);

	var datas = tbCfg.items || []; // 需要显示在页面上的数据
	var pageSize = cruConfig.pageSize;
	if (document.getElementById("pageSize") != null)// 如果有在页面上定义分页条数设置
		pageSize = document.getElementById("pageSize").value;
	if (datas != null)
		for (var i = 0; i < pageSize; i++) {
			var vTr = tbObj.insertRow();
			if (i >= datas.length) {
				for (var j = 0; j < titleRow.cells.length; j++) {
					// 填写空白，补全10条
					var vCell = vTr.insertCell();
					vCell.innerHTML = '&nbsp;';
					if (titleRow.cells(j).isshow == "Hide"
							|| titleRow.cells(j).isshow == "TextHide") {
						vCell.style.display = 'none';
					}
				}
				continue;
			}
			var data = datas[i];
			if (cruConfig.cruAction == 'list2Link') {// 列表页面选择坐父页面某元素的外键
				vTr.onclick = function() {
					eval(rowSelFuncName + "(this)");
				}
				vTr.onmouseover = function() {
					this.className = "trSel";
				}
				vTr.onmouseout = function() {
					this.className = this.initClassName;
				}
			}

			for (var j = 0; j < titleRow.cells.length; j++) {
				var tCell = titleRow.cells(j);
				var vCell = vTr.insertCell();
				var outputValue = getOutputValue(tCell, data);
				var cellValue = outputValue;
				if (tCell.isshow == "Edit") {
					cellValue = "<input type=text onclick=tableInputEditable(this) onkeydown=tableInputkeydown(this) class=rtTableInputReadOnly";
					if (tCell.fieldName != undefined)
						cellValue += " name=" + tCell.fieldName
					else
						cellValue += " name="
								+ tCell.exp.substr(1, tCell.exp.length - 2);
					if (tCell.size != undefined)
						cellValue += " size=" + tCell.size;
					else
						cellValue += " size=8";
					cellValue += " value='" + outputValue + "'>";
					// alert(cellValue);

				} else if (tCell.isshow == "Hide") {
					cellValue = "<input type=text value=" + outputValue;
					if (tCell.fieldName != undefined)
						cellValue += " name=" + tCell.fieldName + ">"
					else
						cellValue += " name="
								+ tCell.exp.substr(1, tCell.exp.length - 2)
								+ ">";
					vCell.style.display = 'none';
				} else if (tCell.isshow == "TextHide") {
					vCell.style.display = 'none';
				}

				if (typeof cellValue == undefined || cellValue == 'undefined')
					cellValue = "";
				if (cellValue == '') {
					cellValue = "&nbsp;";
				} else if (cellValue.indexOf("undefined") != -1) {
					cellValue = cellValue.replace("undefined", "");
				}

				vCell.innerHTML = cellValue;
				// alert(cellValue);
			}
		}
}
/**
 * 获取每个单元格的innerHTML
 */
function getOutputValue(tCell, data) {
	var ret = "";
	var funcArray = [''];
	if (tCell.func != undefined)
		funcArray = tCell.func.split(",");
	// alert(tCell.func);
	if (funcArray[0] == "none") {
		ret = tCell.exp;
	} else {// 替换
		ret = tCell.exp;
		if (ret == undefined)
			return ret;
		var data_id = ret.match(/\{[\w_][\w_\d]*\}/);

		while (data_id != null) {
			data_id = data_id + "";
			var data_value = data[data_id.substr(1, data_id.length - 2)];
			data_value = processData(funcArray, data_value, data_id.substr(1,
							data_id.length - 2));
			pattern = new RegExp(data_id);
			ret = ret.replace(pattern, data_value);
			data_id = ret.match(/\{[\w_][\w_\d]*\}/);
		}
	}
	return ret;
}

// 跳转到第几页
function changePage() {
	var page = getObj(cruConfig.changePage_id).value;
	if (window.event.keyCode == 13) { // 如果用户敲了回车
		if (/^[-]?\d+$/.test(page)) {
			if (page < 1 || page > cruConfig.pageCount) {
				alert("请重新输入合法的页数!");
				getObj(cruConfig.changePage_id).focus();
				return;
			}
			queryData(page);
		} else {
			alert('请输入正整数');
		}
	}
}

function renderTable2() {
	$(".listTable table tr:odd").css("background", "#e2e7f0");
	$(".listTable table tr:even").css("background", "#cbd4e4");
	$(".listTable table tr:eq(0)").css("background", "#bcd1e5");

	$(".listTable table tr").mouseover(function() {
				$(".listTable table tr:odd").hover(function() {
							$(this).css("background", "#f4f7fb");
						}, function() {
							$(this).css("background", "#e2e7f0");
						})
				$(".listTable table tr:even:gt(0)").hover(function() {
							$(this).css("background", "#f4f7fb");
						}, function() {
							$(this).css("background", "#cbd4e4");
						})
			})

}

// 按回车键触发查询按钮
function toSearch() {
	if (event.keyCode == 13) {
		form1.btn_search.click();
	}
}

// 弹出层
function popWindow(url, size) {
	var path = cruConfig.currentPageUrl;
	if (path.indexOf("/epcomp") == 0) { // 如果是组件包里的pmd文件
		path = path.substr("/epcomp".length);
	}
	if (url.indexOf('/') == 0) {
		if (url.indexOf(cruConfig.contextPath) != 0) {
			url = cruConfig.contextPath + url;
		}
	} else {
		path = path.substr(0, path.lastIndexOf("/") + 1);
		if (path.lastIndexOf("/") == (path.length - 1)) {
			url = cruConfig.contextPath + path + url;
		} else
			url = cruConfig.contextPath + path + '/' + url;
	}
	var height = 530;
	var width = 740;
	if (size != null) {
		if (typeof size == 'number') {
			height = size;
		}
		if (typeof size == 'string' && size.indexOf(':') > 0) {
			height = eval(size.split(':')[1]);
			width = eval(size.split(':')[0]);
		}
	}
	dialogOpen("", width, height, url);
}

// 增加查询行，需要重新计算列表所占高度
function addSearchRow() {
	if ($("#ComplexTable tr").length >= 10) {
		alert('对不起，查询条件最多不能超过10个');
	} else {
		$(".table_search tbody").html($(".table_search tbody").html()
				+ $(".table_search tbody tr:last").html());
		/*
		 * if(cruConfig.cruAction == "list2Select"){ dataListCttHeight(); }else{
		 * ctt_autoHeight(); };
		 */
	}
}
//减少查询行
function deleteSearchRow() {
	if ($("#ComplexTable tbody tr").length <= 1) {
		alert('请至少保留1个查询条件！');
	} else {
		var tbObj = getObj("ComplexTable");
		if (tbObj.rows.length == 1) {
			return;
		}
		tbObj.deleteRow(tbObj.rows.length - 1);
		if (cruConfig.cruAction == "list2Select") {
			dataListCttHeight();
		} else {
			ctt_autoHeight();
		};

	}
}

$(function(){
    function forbidBackSpace(e){  
	    var ev = e || window.event;//获取event对象  
	    var obj = ev.target || ev.srcElement;//获取事件源  
	    var t = obj.type || obj.getAttribute('type');//获取事件源类型  
	    if(ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea"){  
	        return false;  
	    }  
  	}  
    //禁止后退键  
    document.onkeydown=forbidBackSpace; 
});
